/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.hpvas.control;

import edu.hpvas.persistence.dao.PersonDAO;
import edu.hpvas.persistence.dao.VeterinarianDAO;
import edu.hpvas.persistence.entity.Person;
import edu.hpvas.persistence.entity.Veterinarian;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author raider
 */
public class Login {
    
    private boolean employee;
    private Person person;
    public static Person currentPerson = new Person();
    
    

    public Login() {}

    public Person getPerson() {
        return person;
    }

    public boolean isEmployee() {
        return employee;
    }

    public void setEmployee(boolean employee) {
        this.employee = employee;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    
    public void createDefaultUser(){
        Person person = new Person();
        person.setId(new Integer(1));
        person.setBirthPlace("Bogota");
        person.setBirthdate(new Date(1992-1900, 5, 25));
        person.setLastName("Laverde");
        person.setName("Nicolas");
        person.setSsn("what ever");
        PersonDAO personDao = new PersonDAO();
        try {
            personDao.create(person);
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        Veterinarian vet = new Veterinarian();
        vet.setIdPerson(person);
        vet.setGraduateAt("Universidad Nacional");
        vet.setSpecialities("save pets");
        vet.setSalary(4500000);
        vet.setId(new Integer(1));
        vet.setFocusOfPractice("the wings of birds");
        VeterinarianDAO vetDao = new VeterinarianDAO();
        try {
            vetDao.create(vet);
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
                
    }
    public boolean authenticate(String name, String lastName){
        
        boolean exist=false;
        Person p=new Person();
        p.setName(name);
        p.setLastName(lastName);
        PersonDAO personDAO=new PersonDAO();
        List<Person> people = personDAO.findPersonEntities();
        if(people.isEmpty()){
            createDefaultUser();
            people = personDAO.findPersonEntities();
        }
        for (Person personAux : people) {
            if(personAux.getName().toLowerCase().equals(name.toLowerCase())
                    &&personAux.getLastName().toLowerCase().equals(lastName.toLowerCase())){
                person=personAux;
                employee=new VeterinarianDAO().findVeterinarian(person.getId())!=null;
                if(!employee){
                    return false;
                }
                exist=true;
                
                
                currentPerson= person;
                break;
            }
        }
        return exist;
    }
}
